import * as vscode from 'vscode';

/**
 * Check whether the position is within the specified label
 * @param document
 * @param position
 * @param tagName Tag name (without angle brackets, such as 'script', 'template')
 * 
 * @returns true or false
 * 
 * @example
 * ```ts
 * // 1. Single label detection
 * TagDetector.isInsideAnyTag(document, position, ['script']);
 * 
 * // 2. Multiple label detection
 * TagDetector.isInsideAnyTag(document, position, ['script', 'template']);
 * 
 * // 3. Customization label detection
 * TagDetector.isInsideAnyTag(document, position, ['my-div']);
 *  
 * ```
 */
export class TagDetector {
    static isInsideAnyTag(
        document: vscode.TextDocument,
        position: vscode.Position,
        tagNames: string[]
    ): boolean {
        const offset = document.offsetAt(position);
        const text = document.getText();
        
        return tagNames.some(tagName => {
            const openTag = text.lastIndexOf(`<${tagName}`, offset);
            if (openTag === -1) { return false;}
            
            const closeTag = text.indexOf(`</${tagName}>`, openTag);
            return !(closeTag === -1 || closeTag < offset);
        });
    }
}